package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description TODO
 * @Author dingchunlin
 * @Date 2023/8/6 21:18
 * @Version 1.0.0
 */
@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult getPage(DishPageQueryDTO dishPageQueryDTO) {
        //首先创建一个pege返回对象
        PageResult result = new PageResult();
        List<DishVO> list = new ArrayList<DishVO>();
        //设置第几页和每页显示条数
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        //进行查询
        Page<Dish> dishPage = dishMapper.getPage(dishPageQueryDTO);
        //将结果添加到pageresult中
        for (Dish dish : dishPage.getResult()) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(dish,dishVO);
            dishVO.setCategoryName(categoryMapper.selectById(dish.getCategoryId()).getName());
            list.add(dishVO);
        }
        result.setTotal(dishPage.getTotal());
        result.setRecords(list);
        return result;
    }
    /**
     * 菜品添加
     * @param dishDTO
     */
    @Override
    public void save(DishDTO dishDTO) {
        //首先创建一个菜品实体类，用来在菜品添加数据时使用
        Dish dish = new Dish();
        //实现属性的拷贝
        BeanUtils.copyProperties(dishDTO,dish);
        //将菜品的状态设为在售，默认为0
        dish.setStatus(StatusConstant.ENABLE);
        //先将菜品进行保存
        dishMapper.save(dish);
        //获取刚刚添加菜品的id用来和口味信息进行关联
        Long id = dish.getId();
        //获取添加菜品的口味信息
        List<DishFlavor> flavors = dishDTO.getFlavors();
        //进行菜品口味信息的保存
        for (DishFlavor flavor : flavors) {
            //将口味和菜品进行关联
            flavor.setDishId(id);
            dishMapper.saveFlavor(flavor);
        }
    }

    @Override
    public Result deleteDish(Long[] ids) {
        for (Long id : ids) {
            Integer integer = setmealMapper.selectDishCount(id);
            Dish dish = dishMapper.selectById(id);
            if (integer>0){
                return Result.error(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }else if(StatusConstant.ENABLE.equals(dish.getStatus())){
                return Result.error(MessageConstant.DISH_ON_SALE);
            }else {
                dishMapper.deleteDishById(id);
                dishMapper.deleteFlavorById(id);
            }
        }
        return Result.success();
    }

    /**
     * 根据id进行菜品信息查询
     * @param id
     * @return
     */
    @Override
    public DishDTO selectById(Long id) {
        DishDTO dishDTO = new DishDTO();
        Dish dish = dishMapper.selectById(id);
        BeanUtils.copyProperties(dish,dishDTO);
        List<DishFlavor> dishFlavors = dishMapper.selectFlavorByDishId(dish.getId());
        dishDTO.setFlavors(dishFlavors);
        return dishDTO;
    }

    /**
     * 根据id修改菜品信息
     * @param dishDTO
     * @return
     */
    @Override
    public void updateById(DishDTO dishDTO) {
        //创建一个实体类用于修改参数
        Dish dish = new Dish();
        //进行属性拷贝
        BeanUtils.copyProperties(dishDTO,dish);
        //执行修改菜品信息
        dishMapper.updateById(dish);
        //将该菜品下的口味信息进行删除
        dishMapper.deleteFlavorById(dish.getId());
        //获取修改后的口味信息
        List<DishFlavor> flavors = dishDTO.getFlavors();
        for (DishFlavor flavor : flavors) {
            //将口味和菜品进行关联
            flavor.setDishId(dishDTO.getId());
            //最后将修改后的菜品口味信息进行添加
            dishMapper.saveFlavor(flavor);
        }
    }

    /**
     * 根据菜品id进行状态修改
     * @param id
     */
    @Override
    public Result updateDishStatus(Long id,Integer status) {
        //首先查询套餐中该商品的数量
        Integer integer = setmealMapper.selectDishCount(id);
        if (integer>0){
            return Result.error(MessageConstant.DISH_ENABLE_FAILED);
        }
        Dish dish = new Dish();
        dish.setStatus(status);
        dish.setId(id);
        dishMapper.updateDishStatus(dish);
        return Result.success();
    }

    /**
     * 根据分类查询菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> selectByCategoryId(Long categoryId) {
        return dishMapper.selectByCategoryId(categoryId);
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.selectByCategoryId(dish.getCategoryId());

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishMapper.selectFlavorByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
